package com.sskj.gcc.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.Spanned;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.gcc.R;
import com.sskj.gcc.bean.GuideListBean;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.box.downloader.MyDownLoader;
import com.zzhoujay.richtext.RichText;
import com.zzhoujay.richtext.callback.BitmapStream;
import com.zzhoujay.richtext.exceptions.HttpResponseCodeException;
import com.zzhoujay.richtext.ig.DefaultImageDownloader;
import com.zzhoujay.richtext.ig.ImageDownloader;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/07/16 17:36:07
 */
@Route(path = RConfig.APP_NOTICE_DETAIL)
public class NoticeDetailActivity extends BaseActivity<NoticeDetailPresenter> {


    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.ivBack)
    LinearLayout ivBack;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.time_tv)
    TextView timeTv;
    //    @BindView(R.id.content_tv)
//    TextView contentTv;
    @BindView(R.id.webView)
    WebView webView;

    @Autowired
    String head;
    @Autowired
    String time;
    @Autowired
    String title;
    @Autowired
    String id;
    /**
     * 资讯会有content
     */
    @Autowired
    String content;

    @Override
    public NoticeDetailPresenter getPresenter() {
        return new NoticeDetailPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        webView.getSettings().setJavaScriptEnabled(true);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_notice_detail;
    }

    @Override
    public void initData() {
        tvTitle.setText(head);
        titleTv.setText(title);
        timeTv.setText(time);
        if (content != null && !content.isEmpty()) {
//            RichText.fromHtml(content)
//                    .into(contentTv);
            content = content.replaceAll("width=\"\\d+\"", "width=\"100%\"").replaceAll("height=\"\\d+\"", "height=\"auto\"");
            String varjs = "<script type='text/javascript'> \nwindow.onload = function()\n{var $img = document.getElementsByTagName('img');" +
                    "for(var p in  $img){$img[p].style.width = '100%'; $img[p].style.height ='auto'}}</script><style>* {font-size:14px;line-height:20px;} p {color:#666;}</style>";
            webView.loadDataWithBaseURL("http://peacockworld.io", varjs + content, "text/html", "UTF-8", null);
        } else {
            mPresenter.getNoticeDetail(id);
        }

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, NoticeDetailActivity.class);
        context.startActivity(intent);
    }

    public void setData(GuideListBean data) {
//        RichText.fromHtml(data.getContent()).into(contentTv);

        String content = data.getContent();
        content = content.replaceAll("width=\"\\d+\"", "width=\"100%\"").replaceAll("height=\"\\d+\"", "height=\"auto\"");
        String varjs = "<script type='text/javascript'> \nwindow.onload = function()\n{var $img = document.getElementsByTagName('img');" +
                "for(var p in  $img){$img[p].style.width = '100%'; $img[p].style.height ='auto'}}</script><style>* {font-size:14px;line-height:20px;} p {color:#666;}</style>";
        webView.loadDataWithBaseURL("http://peacockworld.io", varjs + content, "text/html", "UTF-8", null);
    }
}
